package com.example.demo.controller;

import com.example.demo.datasource.DataSourceContextHolder;
import com.example.demo.datasource.DataSourceUtil;
import com.example.demo.domain.Condition;
import com.example.demo.domain.TableField;
import com.example.demo.exception.BaseException;
import com.example.demo.service.QueryService;
import com.example.demo.vo.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(value = "/api")
public class QueryController {

    @Autowired
    private QueryService queryService;

    @RequestMapping(value = "/{projectName}/{queryName}/{fillName}/execute",
            method = RequestMethod.POST, produces = "application/json")
    public RestResponse execute(@PathVariable(name = "projectName") String projectName,
                                @PathVariable(name = "queryName") String queryName,
                                @RequestBody Map<String, Object> conditions,
                                @PathVariable(name = "fillName") String fillName) {
        try {
            DataSourceUtil.setDB(projectName);
            List<Map> mapList = queryService.executeSql(projectName, queryName, conditions, fillName);
// 分页 用时解开
//            List<Map> mapList1 = queryService.executeSqlTotalCount(projectName, queryName, conditions);
//            int totalcount=0;
//            if (mapList1!=null && mapList1.size()>0){
//                totalcount=Integer.valueOf(mapList1.get(0).get("HREFCOM").toString());
//            }


            DataSourceUtil.clearDb();
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.SUCCESS, mapList);
//            return new RestResponse.DataResponse<>(RestResponse.ResponseType.SUCCESS, mapList,totalcount);
        } catch (BaseException e) {
            return new RestResponse(e);
        } catch (Exception ee) {
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.ERROR, ee.getMessage());
        }
    }

    @RequestMapping(value = "/{projectName}/{queryName}/{fillName}/getConditions", method = RequestMethod.POST)
    public RestResponse getConditions(@PathVariable(name = "projectName") String projectName,
                                      @PathVariable(name = "queryName") String queryName,
                                      @RequestBody Map<String, Object> conditionsSql,
                                      @PathVariable(name = "fillName") String fillName) {
        try {
            DataSourceUtil.setDB(projectName);
            List<Condition> conditions = queryService.getConditions(projectName, queryName, conditionsSql, fillName);
            DataSourceUtil.clearDb();
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.SUCCESS, conditions);
        } catch (BaseException e) {
            return new RestResponse(e);
        } catch (Exception ee) {
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.ERROR, ee.getMessage());
        }
    }

    @RequestMapping(value = "/{projectName}/{queryName}/{fillName}/getTableFields", method = RequestMethod.GET)
    public RestResponse getTableFields(@PathVariable(name = "projectName") String projectName,
                                      @PathVariable(name = "queryName") String queryName,
                                       @PathVariable(name = "fillName") String fillName) {
        try {
            List<TableField> tableFields = queryService.getTableFields(projectName, queryName, fillName);
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.SUCCESS, tableFields);
        } catch (BaseException e) {
            return new RestResponse(e);
        } catch (Exception ee) {
            return new RestResponse.DataResponse<>(RestResponse.ResponseType.ERROR, ee.getMessage());
        }
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(){
        return "Test";
    }
}
